﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LGSVR.Common;
using System.Data;
using LGSVR.BLL.Sys;
using LGSVR.BLL.Terminal;

namespace LGSVR_WEB.sys
{
    public partial class UserCardGroupEdit : DataBasePage
    {
        private int Pid
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["ID"]) ? 0 : int.Parse(Request.QueryString["ID"]);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "out", "<script>alert('系统超时或服务器故障，请退出重新登录！');window.parent.parent.navigate('../Logout.aspx');</script>");
            }
            if (!IsPostBack)
            {
                PageInit();
                hdUserID.Value = Request["userID"];
                hdUserName.Value = Request["userName"];
            }

        }

        void PageInit()
        {

           
            CardGroupBLL CardGroup = new CardGroupBLL();
            DataTable dt = CardGroup.GetAllGroup(0, "");
            ddlCardGroupID.DataSource = dt;
            ddlCardGroupID.DataTextField = "groupName";
            ddlCardGroupID.DataValueField = "groupID";
            ddlCardGroupID.DataBind();

            ddlCardGroupID.Items.Insert(0, new ListItem("-请选择-", ""));


            Dictionary<string, string> dicSingle;
            // 编辑
            if (!string.IsNullOrEmpty(Request.QueryString["action"]) && Request.QueryString["action"].ToLower() == "edit")
            {
                btnSave.Text = "更 新";
                if (Pid != 0)
                {
                    dicSingle = GetSigle("userCardGroup", "ID=" + Request.QueryString["ID"]);

                    if (dicSingle != null)
                    {
                        ddlCardGroupID.SelectedValue = dicSingle["CardGroupID"];
                        txtbeginActID.Text = dicSingle["beginActID"];
                        txtendActID.Text = dicSingle["endActID"];
                      
                        chkstate.Checked = Utils.ToBool(dicSingle["state"]);
                    }
                    else
                    {
                        MsgContent = ShowMessage("数据加载失败。", MessageType.Error, true, false);
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (HasKey("userCardGroup", "CardGroupID", ddlCardGroupID.SelectedValue + "' AND UserID = '" + Request["userID"], Pid))
            {
                MsgContent = ShowMessage("已存在该用户终端组编号，请更换！", MessageType.Error, false, true);
                return;
            }
            else if (Convert.ToInt32(txtbeginActID.Text.Trim()) > Convert.ToInt32(txtendActID.Text.Trim()))
            {
                MsgContent = ShowMessage("起始节目编号不能大于终止节目编号，请检查！", MessageType.Error, false, true);
                return;
            }
            Dictionary<string, string> dicParam;

            dicParam = new Dictionary<string, string>();

            dicParam.Add("CardGroupID", ddlCardGroupID.SelectedValue);
            dicParam.Add("beginActID", txtbeginActID.Text.Trim());
            dicParam.Add("endActID", txtendActID.Text.Trim());
            dicParam.Add("UserID", Request["userID"]);

            if (chkstate.Checked)
            {
                dicParam.Add("state", "1");
            }
            else
            {
                dicParam.Add("state", "0");
            }
                     

            // 编辑
            if (!string.IsNullOrEmpty(Request.QueryString["action"]) && Request.QueryString["action"].ToLower() == "edit")
            {
                if (Utils.IsNumber(Request.QueryString["ID"]))
                {
                    Edit("userCardGroup", dicParam, "ID=" + Pid);

                    if (Result > 0)
                    {
                        MsgContent = ShowMessage("恭喜您，更新成功！", MessageType.Success, false, true);
                        NoRefresh();
                    }
                    else
                    {
                        if (Result == -100)
                        {
                            MsgContent = ShowMessage("对不起，操作失败，程序参数异常。", MessageType.Error, false, true);
                        }
                        else
                        {
                            MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
                        }
                    }
                }
            }
            else  // 新增
            {
                Insert("userCardGroup", dicParam);

                if (Result > 0)
                {
                    MsgContent = ShowMessage("恭喜您，添加成功！", MessageType.Success, false, true);
                    NoRefresh();
                }
                else
                {
                    MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
                }
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["backUrlParam"]))
            {
                Response.Redirect("UserCardGroupMain.aspx?" + Request.QueryString["backUrlParam"] + "&userID=" + hdUserID.Value + "&userName=" + hdUserName.Value, false);
            }
            else
            {
                Response.Redirect("UserCardGroupMain.aspx?UserID=" + hdUserID.Value + "&userName=" + hdUserName.Value, false);
            }
        }
    }
}